
$.extend({
   // Read a page's GET URL variables and return them as an associative array.
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  },
  
  
  createSourceFromUrl: function(){
  	var allVars = $.getUrlVars();
  	var url = "";
  	var title = "";
  	var description = "";
  	var location = "";
  	var parametersFound = 0, key;
  	for (key in allVars) {
  		if(key == "url"){
  			url = allVars[key]	
  			parametersFound++;
  		} else if(key == "title"){
  			title = allVars[key];
  			parametersFound++;	
  		} else if(key == "description"){
  			description = allVars[key];
  			parametersFound++;	
  		} else if(key == "location"){
  			location = allVars[key];
   		}
    }
    if(parametersFound == 3){
		var defaultDasSources = "<tr class='even' class='odd' id='source_1' title='Gene Names'> <td><select class='representation_type'><option value='track'>track</option><option value='mark'>mark</option></select></td> <td><select class='selected_view'><option value='zoom'>zoom</option><option value='chromosome'>chromosome</option><option value='karyotype'>karyotype</option></select></td> <td class='name'>Gene Names</td> <td class='desc'>Ensembl HGNC genes, NCBI36 assembly</td> <td> <input class='url' type='hidden' value='http://das.sanger.ac.uk/das/hgnc36/features'> <a class='delete' rel='#source_1' title='delete source_1' href='.'><img src='img/delete_cross.png' title='Delete'></a></td></tr><tr class='odd' class='even' id='source_2' title='OMIM'> <td><select class='representation_type'><option value='track'>track</option><option value='mark'>mark</option></select></td> <td><select class='selected_view'><option value='zoom'>zoom</option><option value='chromosome'>chromosome</option><option value='karyotype'>karyotype</option></select></td> <td class='name'>OMIM </td> <td class='desc'>Catalogue of Human Genes Involved in Mendelian Genetic Disorders </td> <td> <input class='url' type='hidden' value='http://das.sanger.ac.uk/das/ens_36_omim_genes/features'> <a class='delete' rel='#source_2' title='delete source_2' href='.'><img src='img/delete_cross.png' title='Delete'></a></td></tr><tr class='even' class='odd' id='source_3' title='Redon Clones'> <td><select class='representation_type'><option value='track'>track</option><option value='mark'>mark</option></select></td> <td><select class='selected_view'><option value='zoom'>zoom</option><option value='chromosome'>chromosome</option><option value='karyotype'>karyotype</option></select></td> <td class='name'>Redon Clones</td> <td class='desc'>CNVs reported in normal individuals </td> <td> <input class='url' type='hidden' value='http://das.sanger.ac.uk/das/WGTP_loci_36/features'> <a class='delete' rel='#source_3' title='delete source_3' href='.'><img src='img/delete_cross.png' title='Delete'></a></td></tr><tr class='odd' class='even' id='source_4' title='DGV'> <td><select class='representation_type'><option value='track'>track</option><option value='mark'>mark</option></select></td> <td><select class='selected_view'><option value='zoom'>zoom</option><option value='chromosome'>chromosome</option><option value='karyotype'>karyotype</option></select></td> <td class='name'>DGV </td> <td class='desc'>Normal, Autism and Cancer Variation from literature </td> <td> <input class='url' type='hidden' value='http://das.sanger.ac.uk/das/toronto_poly_36/features'> <a class='delete' rel='#source_4' title='delete source_4' href='.'><img src='img/delete_cross.png' title='Delete'></a></td></tr><tr class='even' class='odd' id='source_5' title='COSMIC'> <td><select class='representation_type'><option value='track'>track</option><option value='mark'>mark</option></select></td> <td><select class='selected_view'><option value='zoom'>zoom</option><option value='chromosome'>chromosome</option><option value='karyotype'>karyotype</option></select></td> <td class='name'>COSMIC</td> <td class='desc'>Database of Somatic Mutations in Cancer</td> <td> <input class='url' type='hidden' value='http://das.sanger.ac.uk/das/cosmic_transcripts_ncbi36/features'> <a class='delete' rel='#source_5' title='delete source_5' href='.'><img src='img/delete_cross.png' title='Delete'></a></td></tr>";
    	var newDasSource = "";
		newDasSource += "<tr id='source_00' class='odd' title='" + decodeURIComponent(title) + "'>";
        newDasSource += " <td><select class='representation_type'><option value='track'>track</option><option value='mark'>mark</option></select></td>";
    	newDasSource += " <td><select class='selected_view'><option value='zoom'>zoom</option><option value='chromosome'>chromosome</option><option value='karyotype'>karyotype</option></select></td>";
        newDasSource += " <td class='name'>" + decodeURIComponent(title) + "</td>";
        newDasSource += " <td class='desc'>" + decodeURIComponent(description) + "</td>";
        newDasSource += " <td>";
        newDasSource += "   <input class='url' type='hidden' value='" + url + "'>";
        newDasSource += "   <a class='delete' rel='#source_10' title='delete source_10' href='.'><img src='img/delete_cross.png' title='Delete'></a> ";
        newDasSource += " </td>";
        newDasSource += "</tr>";
    	var dasSourceList = $('tbody.dasSourceList').html();
    	//$('tbody.dasSourceList').html(dasSourceList + newDasSource);
    	$('tbody.dasSourceList').html(defaultDasSources + newDasSource);
    	populate_sources();  	
    }
    
    if (location.length > 0) {
    	$("input#inputString").val(decodeURIComponent(location));   	   
    } 
  }
});

$(document).ready(function(){
	$.createSourceFromUrl();
});